package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.properties.TcConfig;
import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.guns.sys.modular.system.entity.DsstoreBillRice;
import cn.stylefeng.guns.sys.modular.system.mapper.DsstoreBillRiceMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.DsstoreBillRiceParam;
import cn.stylefeng.guns.sys.modular.system.model.result.DsstoreBillBusType;
import cn.stylefeng.guns.sys.modular.system.model.result.DsstoreBillRiceResult;
import cn.stylefeng.guns.sys.modular.system.service.DsstoreBillRiceService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jiangge
 * @since 2021-06-02
 */
@Service
public class DsstoreBillRiceServiceImpl extends ServiceImpl<DsstoreBillRiceMapper, DsstoreBillRice> implements DsstoreBillRiceService {

    @Autowired
    private TcConfig tcConfig;

    private Logger logger = LoggerFactory.getLogger(DsstoreBillRiceServiceImpl.class);
    private RestTemplate restTemplate = new RestTemplate();

    @Override
    public void add(DsstoreBillRiceParam param) {
        DsstoreBillRice entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(DsstoreBillRiceParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(DsstoreBillRiceParam param) {
        DsstoreBillRice oldEntity = getOldEntity(param);
        DsstoreBillRice newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public DsstoreBillRiceResult findBySpec(DsstoreBillRiceParam param) {
        return null;
    }

    @Override
    public List<DsstoreBillRiceResult> findListBySpec(DsstoreBillRiceParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(DsstoreBillRiceParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(DsstoreBillRiceParam param) {
        return param.getStoreBillDtid();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private DsstoreBillRice getOldEntity(DsstoreBillRiceParam param) {
        return this.getById(getKey(param));
    }

    private DsstoreBillRice getEntity(DsstoreBillRiceParam param) {
        DsstoreBillRice entity = new DsstoreBillRice();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LinkedHashMap<String, Integer> initDsstoreBillRiceData(List<String> dateList) {

        List<String> dateParamList = new ArrayList<>();
        //业务日期
        dateParamList.add("busdate");
        //审核日期-1.业务日期数据 2.修改的数据 3.之前的数据插入
        dateParamList.add("auditDate");

        //某月底可能无法走接口录入数据，此时需要先执行删除语句，然后将业务json数据录入、审核日期对应json数据录入
        for (String dateParam : dateParamList) {
            for (String day : dateList) {
                //业务日期当天的数据先删除掉，改单删单可能存在垃圾数据
                if (dateParam.equals("busdate")) {
                    this.baseMapper.deleteDataByDay(day);
                }
                if (dateParam.equals("auditDate")) {
                    this.baseMapper.deleteDataByDay2(day);
                }
            }
        }

        //数据条数
        LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
        Map<String, DsstoreBillRice> listMap = new HashMap<>();


        for (String dateParam : dateParamList) {

            for (String day : dateList) {

                Long startTime = System.currentTimeMillis();
                String url = "http://btyjt.c3.fxscm.net/cldpoint/getDsstorebill.do?ent=ENT7617&username=" + tcConfig.getUsername() + "&password=" + tcConfig.getPassword() + "&" + dateParam + "=" + day + "&billTypeId=0400_0100_0300";

                String content = restTemplate.getForObject(url, String.class);
                Long respondTime = (System.currentTimeMillis() - startTime) / 1000;

                JSONObject jsonObjectResult = JSON.parseObject(content);
                JSONArray jsonArray = jsonObjectResult.getJSONArray("data");

                /**
                 * ShopCode    ShopName	ItemSmallClassName  ItemName
                 * 没有店名查询的的是所有的店,把获取到的数据取出来便利 然后挨个插入到表中
                 */
                JSONObject jsonObject;
                Integer allDataCount = jsonArray.size();
                List<DsstoreBillRice> thisDayData = Lists.newLinkedList();

                for (int i = 0; i < jsonArray.size(); i++) {
                    //把所有 ItemSmallClassName 为 酱料 的添加到list中
                    jsonObject = jsonArray.getJSONObject(i);
                    List<String> type1 = Lists.newLinkedList();
//                    type1.add("门店统配入库");
//                    type1.add("门店直配入库");
//                    type1.add("门店自采入库");
//                    type1.add("门店其他入库");
//                    type1.add("门店期初单");
//                    type1.add("店间调入");
//                    type1.add("门店盘盈");
                    type1.add("门店盘亏");
//                    type1.add("门店外销退货");
//                    type1.add("门店冲减出库");
//                    type1.add("门店报损出库");
//                    type1.add("门店外销出库");
//                    type1.add("门店统配退货单");
//                    type1.add("门店部门领用");
//                    type1.add("店内档口领用");
//                    type1.add("店间调出");
//                    type1.add("门店其他出库");
//                    type1.add("门店自采退货");

                    DsstoreBillBusType DsstoreBillBusType = new DsstoreBillBusType();
                    DsstoreBillBusType.setBillBusType(jsonObject.getOrDefault("BillBusType", "").toString());
                    DsstoreBillBusType.setBillType(jsonObject.getOrDefault("BillType", "").toString());

                    if ("干货".equals(jsonObject.getOrDefault("ItemSmallClassName", "").toString())
                            && (jsonObject.getOrDefault("ItemName", "").toString()).contains("大米")
                            && type1.contains(jsonObject.getOrDefault("BillType", ""))
                            && !"山西配送中心".equals(jsonObject.getOrDefault("ShopName", "").toString())
                            && !"山东配送中心".equals(jsonObject.getOrDefault("ShopName", "").toString())) {
                        DsstoreBillRice DsstoreBillRice = convertToDsstoreBillRice(jsonObject);
                        DsstoreBillBusType DsstoreBillBusType2 = new DsstoreBillBusType();
                        DsstoreBillBusType2.setBillBusType(DsstoreBillRice.getBillBusType());
                        DsstoreBillBusType2.setBillType(DsstoreBillRice.getBillType());
//                        if (DsstoreBillRice.getShopCode().equals("039")) {
                        listMap.put(DsstoreBillRice.getStoreBillDtid(), DsstoreBillRice);
//                        }
                        thisDayData.add(DsstoreBillRice);
                    }
                }
                logger.info("门店库存流水入库数据：{},数据时间：{},数据总数：{}，满足条件：{}条数据,请求响应时间：{}", dateParam, day, allDataCount, thisDayData.size(), respondTime);
                map.put(day + dateParam, thisDayData.size());
            }
        }

        //分批插入数据
        if (listMap.size() > 0) {
            int currentComplete = 0;
            for (Map.Entry<String, DsstoreBillRice> DsstoreBillRiceEntry : listMap.entrySet()) {
                DsstoreBillRice DsstoreBillRice = DsstoreBillRiceEntry.getValue();
                currentComplete++;
                try {
                    //不存在增加
                    this.saveOrUpdate(DsstoreBillRice);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }

        //删除不在数据范围内的数据
        return map;

    }

    /**
     * @param dateList
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDsstorebillRiceDataByDays(List<String> dateList, int runParam) {

        List<String> dateParamList = new ArrayList<>();
        //业务日期
        dateParamList.add("busdate");
        //审核日期
        dateParamList.add("auditDate");

        //因为存在删单现象，每次保存后将当天不该存在的数据删除
        Map<String, Set<String>> sureHaveDataMap = new HashMap<>();
        Map<String, Set<String>> sureHaveDataMap2 = new HashMap<>();

        Set<String> repetRunDayList = new LinkedHashSet<>();

        for (String dateParam : dateParamList) {
            for (String day : dateList) {


                String url = "http://btyjt.c3.fxscm.net/cldpoint/getDsstorebill.do?ent=ENT7617&username=" + tcConfig.getUsername() + "&password=" + tcConfig.getPassword() + "&" + dateParam + "=" + day + "&billTypeId=0400_0100_0300";
                String content = restTemplate.getForObject(url, String.class);

                JSONObject jsonObjectResult = JSON.parseObject(content);
                JSONArray jsonArray = jsonObjectResult.getJSONArray("data");

                /**
                 * ShopCode    ShopName	ItemSmallClassName  ItemName
                 * 没有店名查询的的是所有的店,把获取到的数据取出来便利 然后挨个插入到表中
                 */
                JSONObject jsonObject;
                DsstoreBillRice dsstoreBill;
                for (int i = 0; i < jsonArray.size(); i++) {
                    //把所有 ItemSmallClassName 为 酱料 的添加到list中
                    jsonObject = jsonArray.getJSONObject(i);
                    List<String> type1 = Lists.newLinkedList();
//                    type1.add("门店统配入库");
//                    type1.add("门店直配入库");
//                    type1.add("门店自采入库");
//                    type1.add("门店其他入库");
//                    type1.add("门店期初单");
//                    type1.add("店间调入");
//                    type1.add("门店盘盈");
                    type1.add("门店盘亏");
//                    type1.add("门店外销退货");
//                    type1.add("门店冲减出库");
//                    type1.add("门店报损出库");
//                    type1.add("门店外销出库");
//                    type1.add("门店统配退货单");
//                    type1.add("门店部门领用");
//                    type1.add("店内档口领用");
//                    type1.add("店间调出");
//                    type1.add("门店其他出库");
//                    type1.add("门店自采退货");

                    if ("干货".equals(jsonObject.getOrDefault("ItemSmallClassName", "").toString())
                            && (jsonObject.getOrDefault("ItemName", "").toString()).contains("大米")
                            && type1.contains(jsonObject.getOrDefault("BillType", ""))
                            && !"山西配送中心".equals(jsonObject.getOrDefault("ShopName", "").toString())
                            && !"山东配送中心".equals(jsonObject.getOrDefault("ShopName", "").toString())) {
                        dsstoreBill = convertToDsstoreBillRice(jsonObject);
                        //删除不在业务日期当天的数据
                        if (dateParam.equals("busdate")) {
                            Set<String> idList = sureHaveDataMap.get(day);
                            if (idList != null) {
                                idList.add(dsstoreBill.getStoreBillDtid());
                                sureHaveDataMap.put(day, idList);
                            } else {
                                Set<String> ids = new LinkedHashSet<>();
                                ids.add(dsstoreBill.getStoreBillDtid());
                                sureHaveDataMap.put(day, ids);
                            }
                        } else if (dateParam.equals("auditDate")) {
                            if (runParam == 0) {
                                String dayStr = DateUtil.getStrDate(dsstoreBill.getBusDate());
                                repetRunDayList.add(dayStr);
                                Set<String> idList = sureHaveDataMap2.get(dayStr);
                                if (idList != null) {
                                    idList.add(dsstoreBill.getStoreBillDtid());
                                    sureHaveDataMap2.put(dayStr, idList);
                                } else {
                                    Set<String> ids = new LinkedHashSet<>();
                                    ids.add(dsstoreBill.getStoreBillDtid());
                                    sureHaveDataMap2.put(dayStr, ids);
                                }
                            }
                        }
                    }
                }
            }
        }


        this.deleteDataByDayAndIds(sureHaveDataMap);
        runParam = runParam + 1;
        if (repetRunDayList.size() > 0) {
            //保证只执行一次
            //this.deleteDsstorebillRiceDataByDays(new ArrayList<>(repetRunDayList), runParam);
        }
    }

    @Override
    public void deleteDsstorebillDataByDelInterface(List<String> dateList) {
        //因为存在删单现象，每次保存后将当天不该存在的数据删除

        String url = "http://btyjt.c3.fxscm.net/cldpoint/getDeleteBillList.do?ent=ENT7617&username=" + tcConfig.getUsername() + "&password=" + tcConfig.getPassword() + "&beginDate=" + dateList.get(0) + "&endDate=" + dateList.get(dateList.size() - 1);
        String content = restTemplate.getForObject(url, String.class);

        JSONObject jsonObjectResult = JSON.parseObject(content);
        JSONArray jsonArray = jsonObjectResult.getJSONArray("data");

        /**
         * ShopCode    ShopName	ItemSmallClassName  ItemName
         * 没有店名查询的的是所有的店,把获取到的数据取出来便利 然后挨个插入到表中
         */
        JSONObject jsonObject;

        List<String> delIds = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            //把所有 ItemSmallClassName 为 酱料 的添加到list中
            jsonObject = jsonArray.getJSONObject(i);

            String storeBillId = jsonObject.get("storeBillId").toString();
            //删除数据
            delIds.add(storeBillId);

        }

        if (CollectionUtil.isNotEmpty(delIds)) {
            if (delIds.size() > 1000) {
                List<List<String>> partitionIdss = Lists.partition(delIds, 1000);
                for (List<String> list : partitionIdss) {
                    this.baseMapper.deleteBatchIds(list);
                }
            } else {
                this.baseMapper.deleteBatchIds(delIds);
            }
        }


    }

    @Transactional(rollbackFor = Exception.class)
    void deleteDataByDayAndIds(Map<String, Set<String>> sureHaveDataMap) {
        //删除不在数据范围内的数据
        if (CollectionUtil.isNotEmpty(sureHaveDataMap)) {
            for (Map.Entry<String, Set<String>> entry : sureHaveDataMap.entrySet()) {
                //应该存在的
                Set IdsSet = entry.getValue();
                List<String> partitionIds = new ArrayList<>(IdsSet);
                //已经存在的
                List<String> allIds = this.baseMapper.selectIdsByDay(entry.getKey());
                //要删除的
                List<String> Idslist = getTwoListDifference(allIds, partitionIds);
                if (CollectionUtil.isNotEmpty(Idslist)) {
                    if (Idslist.size() > 1000) {
                        List<List<String>> partitionIdss = Lists.partition(Idslist, 1000);
                        for (List<String> list : partitionIdss) {
                            this.baseMapper.deleteDataByIds(list);
                        }
                    } else {
                        this.baseMapper.deleteDataByIds(Idslist);
                    }
                }
            }
        }
    }

    public List<String> getTwoListDifference(List<String> totalList, List<String> containsList) {
        HashMap<String, Boolean> map = new HashMap(0);
        for (int i = 0; i < containsList.size(); i++) {
            map.put(containsList.get(i), true);
        }
        containsList.clear();
        for (int i = 0; i < totalList.size(); i++) {
            if (!map.containsKey(totalList.get(i))) {
                containsList.add(totalList.get(i));
            }
        }
        return containsList;
    }

    private DsstoreBillRice convertToDsstoreBillRice(JSONObject jsonObject) {
        DsstoreBillRice DsstoreBillRice = new DsstoreBillRice();
        DsstoreBillRice.setBillNo(jsonObject.getOrDefault("BillNO", "").toString());
        DsstoreBillRice.setOtherSideName(jsonObject.getOrDefault("OtherSideName", "").toString());
        DsstoreBillRice.setShopName(jsonObject.getOrDefault("ShopName", "").toString());
        DsstoreBillRice.setItemBigClassName(jsonObject.getOrDefault("ItemBigClassName", "").toString());
        DsstoreBillRice.setItemAssistNo(jsonObject.getOrDefault("ItemAssistNO", "").toString());
        DsstoreBillRice.setItemSpec(jsonObject.getOrDefault("ItemSpec", "").toString());
        DsstoreBillRice.setSellTaxes(convertToDecimal(jsonObject.getOrDefault("SellTaxes", "").toString()));
        DsstoreBillRice.setItemSmallClassCode(jsonObject.getOrDefault("ItemSmallClassCode", "").toString());
        DsstoreBillRice.setArriveMoney(convertToDecimal(jsonObject.getOrDefault("ArriveMoney", "").toString()));
        DsstoreBillRice.setStoreCode(jsonObject.getOrDefault("StoreCode", "").toString());
        DsstoreBillRice.setOutputRate(convertToDecimal(jsonObject.getOrDefault("outputRate", "").toString()));
        DsstoreBillRice.setStoreBillRemark(jsonObject.getOrDefault("StoreBillRemark", "").toString());
        DsstoreBillRice.setOutMainAmount(convertToDecimal(jsonObject.getOrDefault("OutMainAmount", "").toString()));
        DsstoreBillRice.setItemSign(jsonObject.getOrDefault("ItemSign", "").toString());
        DsstoreBillRice.setShopCode(jsonObject.getOrDefault("ShopCode", "").toString());
        DsstoreBillRice.setTaxMoney(convertToDecimal(jsonObject.getOrDefault("TaxMoney", "").toString()));
        DsstoreBillRice.setStoreBillDtid(jsonObject.getOrDefault("StoreBillDTID", "").toString());
        DsstoreBillRice.setModifyDate(convertToDate(jsonObject.getOrDefault("modifyDate", "").toString()));
        DsstoreBillRice.setStoreName(jsonObject.getOrDefault("StoreName", "").toString());
        DsstoreBillRice.setStoreBillidRemark(jsonObject.getOrDefault("StoreBillIDRemark", "").toString());
        DsstoreBillRice.setMainUnit(jsonObject.getOrDefault("MainUnit", "").toString());
        DsstoreBillRice.setGiftFlag(convertBooleanToInt(jsonObject.getOrDefault("GiftFlag", "").toString()));
        DsstoreBillRice.setOutStoreMoney(convertToDecimal(jsonObject.getOrDefault("OutStoreMoney", "").toString()));
        DsstoreBillRice.setItemId(jsonObject.getOrDefault("ItemID", "").toString());
        DsstoreBillRice.setItemSmallClassName(jsonObject.getOrDefault("ItemSmallClassName", "").toString());
        DsstoreBillRice.setStoreBillReState(convertBooleanToInt(jsonObject.getOrDefault("StoreBillReState", "").toString()));
        DsstoreBillRice.setBusUser(jsonObject.getOrDefault("BusUser", "").toString());
        DsstoreBillRice.setTaxes(convertToDecimal(jsonObject.getOrDefault("Taxes", "").toString()));
        DsstoreBillRice.setBatchCode(jsonObject.getOrDefault("BatchCode", "").toString());
        DsstoreBillRice.setInStoreMoney(convertToDecimal(jsonObject.getOrDefault("InStoreMoney", "").toString()));
        DsstoreBillRice.setIsOut(convertToInt(jsonObject.getOrDefault("isOut", "").toString()));
        DsstoreBillRice.setShopId(jsonObject.getOrDefault("ShopID", "").toString());
        DsstoreBillRice.setMakeTime(convertToDate(jsonObject.getOrDefault("MakeTime", "").toString()));
        DsstoreBillRice.setBillBusType(jsonObject.getOrDefault("BillBusType", "").toString());
        DsstoreBillRice.setProcessMoney(convertToDecimal(jsonObject.getOrDefault("processMoney", "").toString()));
        DsstoreBillRice.setItemCode(jsonObject.getOrDefault("ItemCode", "").toString());
        DsstoreBillRice.setPushOutStoreMoney(convertToDecimal(jsonObject.getOrDefault("PushOutStoreMoney", "").toString()));
        DsstoreBillRice.setInMainAmount(convertToDecimal(jsonObject.getOrDefault("InMainAmount", "").toString()));
        DsstoreBillRice.setPushOutStoreAmount(convertToDecimal(jsonObject.getOrDefault("PushOutStoreAmount", "").toString()));
        DsstoreBillRice.setStoreBillId(jsonObject.getOrDefault("StoreBillID", "").toString());
        DsstoreBillRice.setSellIncludeTaxMoney(convertToDecimal(jsonObject.getOrDefault("SellIncludeTaxMoney", "").toString()));
        DsstoreBillRice.setOutBusAmount(convertToDecimal(jsonObject.getOrDefault("OutBusAmount", "").toString()));
        DsstoreBillRice.setOtherSideType(jsonObject.getOrDefault("OtherSideType", "").toString());
        DsstoreBillRice.setPushInStoreMoney(convertToDecimal(jsonObject.getOrDefault("PushInStoreMoney", "").toString()));
        DsstoreBillRice.setYield(convertToDecimal(jsonObject.getOrDefault("yield", "").toString()));
        DsstoreBillRice.setArriveAmount(convertToDecimal(jsonObject.getOrDefault("ArriveAmount", "").toString()));
        DsstoreBillRice.setItemName(jsonObject.getOrDefault("ItemName", "").toString());
        DsstoreBillRice.setSysBatchCode(jsonObject.getOrDefault("SysBatchCode", "").toString());
        DsstoreBillRice.setCreateDate(convertToDate(jsonObject.getOrDefault("createDate", "").toString()));
        DsstoreBillRice.setAuditDate(convertToDate(jsonObject.getOrDefault("auditDate", "").toString()));
        DsstoreBillRice.setOtherSideId(jsonObject.getOrDefault("OtherSideID", "").toString());
        DsstoreBillRice.setBillType(jsonObject.getOrDefault("BillType", "").toString());
        DsstoreBillRice.setSellTaxMoney(convertToDecimal(jsonObject.getOrDefault("SellTaxMoney", "").toString()));
        DsstoreBillRice.setBillBusTypeId(jsonObject.getOrDefault("BillBusTypeId", "").toString());
        DsstoreBillRice.setPushInStoreAmount(convertToDecimal(jsonObject.getOrDefault("PushInStoreAmount", "").toString()));
        DsstoreBillRice.setInBusAmount(convertToDecimal(jsonObject.getOrDefault("InBusAmount", "").toString()));
        if (DsstoreBillRice.getStoreBillDtid().equals("2c910a61768f5d0f0176c193f3d34e46")) {
            System.out.println();
        }

        if (DsstoreBillRice.getStoreBillDtid().equals("2c910a61768f5d0f0176c193f3d34e46")) {
            System.out.println();
        }

        DsstoreBillRice.setBusDate(convertToDate(jsonObject.getOrDefault("busDate", "").toString()));
        DsstoreBillRice.setAPYear(convertToLong(jsonObject.getOrDefault("APYear", "").toString()));
        DsstoreBillRice.setItemBigClassCode(jsonObject.getOrDefault("ItemBigClassCode", "").toString());
        DsstoreBillRice.setSaleMoney(convertToDecimal(jsonObject.getOrDefault("SaleMoney", "").toString()));
        DsstoreBillRice.setStoreId(jsonObject.getOrDefault("StoreID", "").toString());
        DsstoreBillRice.setItemAbc(jsonObject.getOrDefault("ItemAbc", "").toString());
        DsstoreBillRice.setArrivalDate(convertToDate(jsonObject.getOrDefault("ArrivalDate", "").toString()));
        DsstoreBillRice.setAPMonth(convertToInt(jsonObject.getOrDefault("APMonth", "").toString()));
        DsstoreBillRice.setOtherSideCode(jsonObject.getOrDefault("OtherSideCode", "").toString());
        DsstoreBillRice.setBusUnit(jsonObject.getOrDefault("BusUnit", "").toString());
        DsstoreBillRice.setIncludeTaxMoney(convertToDecimal(jsonObject.getOrDefault("IncludeTaxMoney", "").toString()));
        DsstoreBillRice.setParentBillNo(jsonObject.getOrDefault("ParentBillNO", "").toString());
        return DsstoreBillRice;
    }

    public static Date convertToDate(String time) {
        if (StringUtils.isBlank(time)) {
            return null;
        }
        Date date1 = new Date(time);
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdf1.format(date1);

        Date date = null;
        try {
            date = sdf1.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 转换数字为decimal
     *
     * @param data 参数
     * @return
     */
    public static BigDecimal convertToDecimal(String data) {
        if (StringUtils.isBlank(data) || "0E-8".equals(data) || "0-8".equals(data) || "0E-12".equals(data) || "0e-12".equals(data)) {
            return BigDecimal.ZERO;
        }
        BigDecimal decimal = new BigDecimal(data);
        return decimal;
    }

    public static Integer convertBooleanToInt(String flag) {
        if (flag.equals("false")) {
            return 0;
        } else if (flag.equals("true")) {
            return 1;
        }
        return null;
    }

    public static Integer convertToInt(String data) {
        if (StringUtils.isBlank(data) || "0E-8".equals(data) || "0-8".equals(data) || "0E-12".equals(data) || "0e-12".equals(data)) {
            return 0;
        }
        Integer integer = Integer.valueOf(data);
        return integer;
    }

    public static Long convertToLong(String data) {
        if (StringUtils.isBlank(data) || "0E-8".equals(data) || "0-8".equals(data) || "0E-12".equals(data) || "0e-12".equals(data)) {
            return 0L;
        }
        Long lon = Long.valueOf(data);
        return lon;
    }

    @Override
    public void readFileAndSaveData(JSONObject jsonObject) throws IOException {
        //把所有 ItemSmallClassName 为 酱料 的添加到list中
        List<String> type1 = Lists.newLinkedList();

        type1.add("门店盘亏");

        DsstoreBillBusType DsstoreBillBusType = new DsstoreBillBusType();
        DsstoreBillBusType.setBillBusType(jsonObject.getOrDefault("BillBusType", "").toString());
        DsstoreBillBusType.setBillType(jsonObject.getOrDefault("BillType", "").toString());

        if ("干货".equals(jsonObject.getOrDefault("ItemSmallClassName", "").toString())
                && (jsonObject.getOrDefault("ItemName", "").toString()).contains("大米")
                && type1.contains(jsonObject.getOrDefault("BillType", ""))
                && !"山西配送中心".equals(jsonObject.getOrDefault("ShopName", "").toString())
                && !"山东配送中心".equals(jsonObject.getOrDefault("ShopName", "").toString())) {
            DsstoreBillRice DsstoreBillRice = convertToDsstoreBillRice(jsonObject);
            this.saveOrUpdate(DsstoreBillRice);
        }
    }

}
